package com.ctg.behavior.calc.impala.builder.funnel;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import com.ctg.behavior.calc.impala.report.funnel.CtCalcFunnelStepReport;
import com.ctg.behavior.common.funnel.CtFunnelCalcBody;
import com.ctg.behavior.common.funnel.CtFunnelDetailBody;
import com.ctg.behavior.common.funnel.CtFunnelStep;

@Component
public class CtFunnelStepReprotBuilder {

    public List<CtCalcFunnelStepReport> doBuilder(CtFunnelCalcBody funnelCalcBody) {
        List<CtCalcFunnelStepReport> steps = new ArrayList<>();
        CtFunnelDetailBody funnelDetailBody = funnelCalcBody.getFunnelDetail();
        List<CtFunnelStep> funnelSteps = funnelDetailBody.getSteps();
        List<String> eventIds = new ArrayList<>();
        List<String> eventCNames = new ArrayList<>();

        Integer stepNo = 0;
        for (CtFunnelStep step : funnelSteps) {
        	//封装eventId
        	eventIds.add(step.getEventId());
            if(StringUtils.isEmpty(step.getEventCName())) {
                eventCNames.add(step.getEventId());
            } else {
                eventCNames.add(step.getEventCName());
            }
            CtCalcFunnelStepReport funnelStepReport = new CtCalcFunnelStepReport();
            funnelStepReport.setFilter(step.getFilter());
            funnelStepReport.setFromDate(funnelCalcBody.getFromDate());
            funnelStepReport.setToDate(funnelCalcBody.getToDate());
            funnelStepReport.setByFields(funnelCalcBody.getByFields());
            funnelStepReport.setEventId(step.getEventId());
            funnelStepReport.setGlobalFilter(funnelCalcBody.getFilter());
            funnelStepReport.setEventIds(eventIds);
            funnelStepReport.setEventCNames(eventCNames);
            
            funnelStepReport.setAppId(funnelCalcBody.getAppId());
            funnelStepReport.setStepNo(stepNo);
            
            steps.add(funnelStepReport);
            
            stepNo++;
        }
        return steps;
    }

}
